package com.hbxx.lotteryapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbxx.lotteryapp.commons.Result;
import com.hbxx.lotteryapp.commons.TokenService;
import com.hbxx.lotteryapp.entity.LoginUser;
import com.hbxx.lotteryapp.entity.Order;
import com.hbxx.lotteryapp.mapper.OrderMapper;
import com.hbxx.lotteryapp.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tonny
 * @since 2022-06-08
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private TokenService tokenService;

    /**
     * 订单分页查询
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @Override
    public Result pageList(Integer pageNum, Integer pageSize, HttpServletRequest request) {

        LoginUser loginUser = tokenService.getLoginUser(request);
        String openId = loginUser.getOpenId();
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("open_id",openId).orderByDesc("pay_time");
        Page<Order> orderPage = new Page<>(pageNum, pageSize);
        Page<Order> selectPage = orderMapper.selectPage(orderPage, orderQueryWrapper);
        return Result.SUCCESS(selectPage);
    }

    @Override
    public Result detail(String orderCode) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_code",orderCode);
        Order order = orderMapper.selectOne(orderQueryWrapper);
        return Result.SUCCESS(order);
    }
}
